const regeneratorRuntime = require("../../vendor/regenerator-runtime")
const co = require("../../vendor/co")


function getUserInfo(checkServer = true) {
    const ps = getCurrentPages()
    const current = ps[ps.length - 1]
    current.setData({
        isButtonUserInfo: wx.canIUse("button.open-type.getUserInfo")
    })
    if (!current._getUser) {
        current._getUser = evt => {
            evt.detail.userInfo && getApp().http.post("city/member/save-info", {
                data: evt.detail.userInfo,
                showMsg: false
            })
            current.setData({
                noAuth: evt.detail.userInfo ? 0 : 1
            })
        }
    }
    current._getUserPolyfill = evt => {
        if (!wx.canIUse("button.open-type.getUserInfo")) {
            wx.openSetting && wx.openSetting({
                success: res => {
                    console.log(res, 'xx')
                    if (res.authSetting["scope.userInfo"]) {
                        wx.getUserInfo({
                            success: user => {
                                current.setData({
                                    noAuth: 0
                                })
                                getApp().http.post("city/member/save-info", {
                                    data: user.userInfo,
                                    showMsg: false,

                                })
                            }
                        })
                    }
                }
            })
        }
    }
    return co(function* () {
        let user;
        if (checkServer) {
            //检查用户是否授权
            user = yield getApp().http.get("city/member/get-info")
            if (user.data.nickname || user.data.images) {
                wx.setStorage({
                    key: 'user',
                    data: {
                        nickName: user.data.nickname,
                        avatarUrl: user.data.images
                    },
                })
                return {
                    nickName: user.data.nickname,
                    avatarUrl: user.data.images
                }
            }
        }

        //拉取用户信息
        try {
            user = yield getApp().toPromise(wx.getUserInfo)
            if (user.userInfo) {
                //授权成功，保存至本地缓存
                wx.setStorage({
                    key: 'user',
                    data: user.userInfo
                })
                //保存至服务器
                getApp().http.post("city/member/save-info", {
                    data: user.userInfo,
                    showMsg: false
                })
                return user.userInfo
            } else {
                current.setData({
                    noAuth: 1
                })
                return Promise.reject("授权失败")
            }
        } catch (err) {
            current.setData({
                noAuth: 1
            })
            return Promise.reject("授权失败")
        }

    })
}

module.exports = getUserInfo